//
//  AppDelegate.m
//  ocdemo
//
//  Created by mac on 2024/8/15.
//

#import "AppDelegate.h"

#import "ScriptLoadUtil.h"
#import <React/RCTBundleURLProvider.h>
#import "ReactController.h"
#import "YSDevicesVC.h"
#import "YSShopMallVC.h"
#import "YSMineVC.h"
#import "RCTBridge+CustomerBridge.h"
#import "TabbarManager.h"
@interface AppDelegate ()
{
    RCTBridge*bridge;
    UINavigationController*rootViewController;
    UIViewController*mainViewController;
    BOOL isBuzLoaded;
    BOOL isBuz1Loaded;
    BOOL isBuz2Loaded;
    BOOL isBuz3Loaded;
    
}
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 1. 移除所有可能干扰的图片
    [UITabBar appearance].backgroundImage = [UIImage new];
    [UITabBar appearance].shadowImage = [UIImage new];

    // 2. 关闭半透明
    [UITabBar appearance].translucent = NO;

    // 3. 分版本设置颜色
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *appearance = [[UITabBarAppearance alloc] init];
        [appearance configureWithOpaqueBackground];
        appearance.backgroundColor = [UIColor whiteColor];
        [UITabBar appearance].standardAppearance = appearance;
        
        if (@available(iOS 15.0, *)) {
            [UITabBar appearance].scrollEdgeAppearance = appearance;
        }
    } else {
        [UITabBar appearance].barTintColor = [UIColor whiteColor];
    }
    
    
    BOOL debugable=[ScriptLoadUtil isDebugable];
    NSURL *jsCodeLocation;
    if(debugable){
        jsCodeLocation=[[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
    }else{
        jsCodeLocation=[[NSBundle mainBundle] URLForResource:
        @"core" withExtension:@"jsbundle"];
    }
 
    bridge=[[RCTBridge alloc]initWithBundleURL:jsCodeLocation moduleProvider:nil launchOptions:launchOptions];
    [ScriptLoadUtil init:bridge];
   
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                                 selector:@selector(onJavaScriptDidLoad:)
//                                                     name:RCTJavaScriptDidLoadNotification
//                                                   object:bridge];


    self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

 
    UITabBarController *tabVc=[[UITabBarController alloc]init];
    tabVc.delegate=self;
    UINavigationController*nav=[[UINavigationController alloc]initWithRootViewController:tabVc];
    [nav setNavigationBarHidden:YES];
    // 确保返回手势可用
      if (nav.interactivePopGestureRecognizer) {
          nav.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
          nav.interactivePopGestureRecognizer.enabled = YES;
      }
    
    
    
    self.navVc=nav;
    self.tabVc=tabVc;

    //首页
    YSDevicesVC *devicesVC=[[YSDevicesVC alloc]init];
    UINavigationController*devicesNav=[[UINavigationController alloc]initWithRootViewController:devicesVC];
    //商城
    YSShopMallVC *shopMallVC=[[YSShopMallVC alloc]init];
    //预加载
    [shopMallVC view];
    UINavigationController*shopMallNav=[[UINavigationController alloc]initWithRootViewController:shopMallVC];
    //我的
    YSMineVC *mineVc=[[YSMineVC alloc]init];
    UINavigationController*mineVcNav=[[UINavigationController alloc]initWithRootViewController:mineVc];
    
    [tabVc setViewControllers:@[devicesVC,shopMallVC,mineVc]];
    

    self.window.rootViewController=nav;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}
//-(void)onJavaScriptDidLoad:(NSNotification *)notification{
//    NSLog(@"Main bundle finished loading.");
//   
//    Boolean isloading= [ScriptLoadUtil isLoading];
//    NSLog(@"isloading %@",isloading?@"YES":@"NO");
//
//    // 确保 bridge 已准备好
//      if (!bridge || !bridge.batchedBridge) {
//          NSLog(@"Bridge not ready!");
//          return;
//      }
//    
////    [ScriptLoadUtil loadScriptWithBridge:  [ ScriptLoadUtil getBridge] path:@"pub.jsbundle" moduleName:@"Pub" mainBundle:true];
//    
//    NSURL *jsCodeLocationBuz = [[NSBundle mainBundle] URLForResource:@"RnIndex" withExtension:@"jsbundle"];
//    NSError *error = nil;
//    NSData *sourceBuz = [NSData dataWithContentsOfFile:jsCodeLocationBuz.path
//                                               options:NSDataReadingMappedIfSafe
//                                                 error:&error];
//}
//-(void)goBuz11:(UIButton*)btn{
//    NSLog(@"11");
//    [self gotoBuzWithModuleName:@"RNIndex" bundleName:@"RnIndex.jsbundle"];
//    
//}
//-(void)goBuz2:(UIButton*)btn{
//    NSLog(@"2");
//}
//-(void)goBuz3:(UIButton*)btn{
//    NSLog(@"33");
//}
//-(void)gotoBuzWithModuleName:(NSString*)moduleName bundleName:(NSString*)bundleName{
//    BOOL isBundleLoaded=NO;
//    if([ScriptLoadUtil isDebugable]){
//        isBundleLoaded=YES;
//        
//    }
//    BundleType type=InApp;
//    NSString* bundleUrl=@"";
//    if([bundleName isEqualToString:@"index2.bundle"]){
//        bundleUrl=@"https://github.com/smallnew/react-native-multibundler/raw/master/remotebundles/index2.ios.bundle.zip";
//        type=NetWork;
//        
//    }
//    
//    UIViewController*controller=nil;
//    controller=[[ReactController alloc]initWithURL:bundleUrl path:bundleName type:type moduleName:moduleName initialProperties:@{}];
//    [mainViewController.navigationController pushViewController:controller animated:YES];
//    
//}
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//    tabBarController.viewControllers
//    tabBarController.selectedIndex
    NSInteger selectIndex=(NSInteger) tabBarController.selectedIndex;
    TabbarManager *tabbar=  [bridge moduleForClass:[TabbarManager class]];
    [tabbar didSelecTabbarWithIndex:selectIndex];
}

@end
